import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import axios from 'axios'
import App from './App.vue'

// 引入Element Plus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

// 引入Bootstrap
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.bundle.min.js'

// 设置axios基础URL和默认配置
axios.defaults.baseURL = 'http://localhost:8081'
axios.defaults.withCredentials = true
axios.defaults.headers.common['Content-Type'] = 'application/json'

// 配置请求拦截器
axios.interceptors.request.use(
  config => {
    // 在发送请求之前做些什么
    return config
  },
  error => {
    // 对请求错误做些什么
    return Promise.reject(error)
  }
)

// 配置响应拦截器
axios.interceptors.response.use(
  response => {
    // 对响应数据做点什么
    return response
  },
  error => {
    // 对响应错误做点什么
    console.error('请求错误:', error)
    return Promise.reject(error)
  }
)

// 导入页面组件
import Home from './pages/Home.vue'
import Login from './pages/Login.vue'
import Register from './pages/Register.vue'
import UserInfo from './pages/user/UserInfo.vue'
import CourseList from './pages/course/CourseList.vue'
import AiChat from './pages/ai/AiChat.vue'
import CampusCard from './pages/campus-card/CampusCard.vue'
import DormRepair from './pages/dorm-repair/DormRepair.vue'
import LeaveApply from './pages/leave-apply/LeaveApply.vue'
import Library from './pages/library/Library.vue'
import Notices from './pages/notice/Notices.vue'
import Scholarship from './pages/scholarship/Scholarship.vue'
import Canteen from './pages/canteen/Canteen.vue'
import Activities from './pages/activity/Activities.vue'

// 配置路由
const routes = [
  { 
    path: '/', 
    component: Home,
    meta: { requiresAuth: true }
  },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { 
    path: '/user/info', 
    component: UserInfo,
    meta: { requiresAuth: true }
  },
  { 
    path: '/courses', 
    component: CourseList,
    meta: { requiresAuth: true }
  },
  { 
    path: '/ai/chat', 
    component: AiChat,
    meta: { requiresAuth: true }
  },
  { 
    path: '/campus-card', 
    component: CampusCard,
    meta: { requiresAuth: true }
  },
  { 
    path: '/dorm-repair', 
    component: DormRepair,
    meta: { requiresAuth: true }
  },
  { 
    path: '/leave-apply', 
    component: LeaveApply,
    meta: { requiresAuth: true }
  },
  { 
    path: '/library', 
    component: Library,
    meta: { requiresAuth: true }
  },
  { 
    path: '/notices', 
    component: Notices,
    meta: { requiresAuth: true }
  },
  { 
    path: '/scholarship', 
    component: Scholarship,
    meta: { requiresAuth: true }
  },
  { 
    path: '/canteen', 
    component: Canteen,
    meta: { requiresAuth: true }
  },
  { 
    path: '/activities', 
    component: Activities,
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  // 检查路由是否需要认证
  if (to.meta.requiresAuth) {
    // 检查是否有登录token
    const token = localStorage.getItem('token')
    if (token) {
      // 已登录，允许访问
      next()
    } else {
      // 未登录，重定向到登录页
      next('/login')
    }
  } else {
    // 不需要认证的路由，直接访问
    next()
  }
})

// 创建Vue应用
const app = createApp(App)
app.use(router)
app.use(ElementPlus)
app.mount('#app')